<template>
  <div class="WorkingSchedule" v-loading="loading">
    <el-form ref="form" :model="form" label-position="top" class="myform">
      <div>
        <el-form-item label="工厂用户名" class="myform-item">
          <el-input
            v-model="form.name"
            placeholder="请输入工厂用户名"
            @keyup.enter.native="onSearch"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="状态" class="myform-item">
          <el-select
            v-model="form.status"
            placeholder="请选择"
          >
            <el-option
              v-for="item in listStatus"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </div>
      <div>
        <el-form-item style="margin-top: 50px">
          <el-button type="primary" @click="onSearch">查询</el-button>
          <!-- <el-button type="primary">导出</el-button>
          <el-button type="primary">上传文件</el-button> -->
        </el-form-item>
      </div>
    </el-form>
    <el-table :data="tableData" style="width: 100%" border>
      <el-table-column label="工厂名称" align="center" prop="factory_name">
      </el-table-column>
      <el-table-column label="工厂用户名称" align="center" prop="name">
      </el-table-column>
      <el-table-column label="工厂小程序" align="center" prop="home_level_name">
      </el-table-column>
      <el-table-column label="工厂客户端" align="center" prop="pc_level_name">
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="{ row }">
          <div :style="{ color: row.status == 1 ? 'red' : '#67C23A' }">
            {{ row.status == 0 ? "正常" : "禁用" }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="{ row }">
          <el-button
            type="text"
            style="color: red; margin-right: 10px"
            @click="onDisable(row.id,1)"
            v-if="row.status == 0"
            >禁用</el-button
          >
          <el-button
            type="text"
            style="margin-right: 10px"
            @click="onDisable(row.id,0)"
            v-if="row.status != 0"
            >启用</el-button
          >
          <el-button
            type="text"
            v-if="row.status == 0"
            size="default"
            @click="onEdit(row.id)"
            >编辑身份</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :form="form"
      :total="total"
      @paginationPostOrderTaskList="postAdminCodeGetFactoryUserList"
    />
  </div>
</template>

<script>
import {
  postAdminCodeGetFactoryUserList,
  postAdminCodeSetUserInfotovoid,
} from "@/api";
import pagination from "@/components/pagination.vue";
export default {
  components: { pagination },
  data() {
    return {
      loading: false,
      listStatus: [
        {
          value: "0",
          label: "正常",
        },
        {
          value: "1",
          label: "禁用",
        },
      ],
      form: {
        name: "",
        page: 1,
        size: 10,
      },
      tableData: [],
      total: 1,
    };
  },
  activated() {
    let form = this.form;
    this.postAdminCodeGetFactoryUserList(form);
  },
  methods: {
    async onDisable(id,type) {
      let data = {
        id,
      };
      this.$confirm(`此操作将${type == 1 ?'禁用':'启用'}该工厂用户, 是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          try {
            await postAdminCodeSetUserInfotovoid(data);
            let form = this.form;
            this.postAdminCodeGetFactoryUserList(form);
          } catch (error) {}
        })
        .catch(() => {});
    },
    // 编辑
    onEdit(id) {
      this.$router.push({ name: "PlantDetail", query: { id } });
    },
    // 采购列表
    async postAdminCodeGetFactoryUserList(data) {
      this.loading = true;
      this.form = data;
      postAdminCodeGetFactoryUserList(data)
        .then((res) => {
          this.loading = false;
          console.log("采购列表", res);
          this.tableData = res.data;
          this.total = Number(res.total);
        })
        .catch((err) => {
          this.loading = false;
        });
    },

    // 搜索
    onSearch() {
      this.form.page = 1;
      this.form.size = 10;
      this.postAdminCodeGetFactoryUserList(this.form);
    },
  },
};
</script>

<style lang="scss" scoped>
.myform {
  display: flex;
  justify-content: space-between;
  > div {
    display: flex;
  }
  &-item {
    margin-right: 10px;
  }
}
.WorkingSchedule {
  background: #ffff;
  padding: 20px;
  border-radius: 2px;
  @include boxShadow;
}
</style>
